﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightGraph.Graph;

namespace SilverlightGraph.Library
{
    public class NetworkGenerator
    {
        Random random = new Random();
        List<IEntity> entityRegister;
        List<string> names;
        int depth = 6;
        int maxRandomConnections = 3;
        int maxChildren = 3;

        public NetworkGenerator()
        {

        }

        public int MaxRandomConnections
        {
            get { return this.maxRandomConnections; }
            set { this.maxRandomConnections = value; }
        }

        public int MaxChildren
        {
            get { return this.maxChildren; }
            set { this.maxChildren = value; }
        }

        public int Depth
        {
            get { return this.depth; }
            set { this.depth = value; }
        }

        public IEntity CreateGraph(Graph1 graph)
        {
            this.entityRegister = new List<IEntity>();
            IEntity entity = null;
            List<string> nodes = new List<string>();

            if (graph != null)
            {
                foreach (Node n in graph.Nodes)
                {
                    GenericEntity newEntity = new GenericEntity(n.Name, n.Label, n.Width, n.Height, n.X, n.Y);
                    nodes.Add(n.Name);
                    this.entityRegister.Add(newEntity);
                }

                foreach (Edge e in graph.Edges)
                {
                    int index1 = nodes.IndexOf(e.Source);
                    int index2 = nodes.IndexOf(e.Target.Replace("\"", ""));

                    this.entityRegister[index1].Connections.Add(this.entityRegister[index2]);
                }
                return this.entityRegister[0];
            }
            return entity;
        }

        void CreateNetworkConnections()
        {
            foreach (IEntity entity in this.entityRegister)
            {
                this.CreateRandomConnections(entity);
            }
        }

        void CreateRandomConnections(IEntity entity)
        {
            int count = random.Next(0, this.MaxRandomConnections);
            if (count == 0)
            {
                return;
            }

            for (int i = 0; i < count; i++)
            {
                int index = random.Next(this.entityRegister.Count);
                IEntity target = this.entityRegister[index];
                this.TryAssociation(entity, target);
            }
        }

        void TryAssociation(IEntity entity, IEntity target)
        {
            if (entity == target)
            {
                return;
            }

            if (entity.Connections.Contains(target))
            {
                return;
            }

            if (target.Connections.Contains(entity))
            {
                return;
            }

            entity.Connections.Add(target);
            target.Connections.Add(entity);
        }


    }
}
